<?php
namespace app\index\model;

use think\Db;

class DatamanufactureModel
{
    /**
     * 获取sku数据
     * @author LSH
     * @return array
     */
    public static function dbGetSkuInfo($sku)
    {
        return Db::table('skus')
            ->alias('s')
            ->join('sku_attribute t','s.sku = t.sku','LEFT')
            ->join('sku_handover h','s.sku = h.sku','LEFT')
            ->field('
                h.*,
                s.*,
                CASE 
                    s.sales_status
                    WHEN 1 THEN "正常在售"
                    WHEN 2 THEN "清库存"
                    WHEN 3 THEN "缺货"
                    WHEN 4 THEN "下架"
                    WHEN 5 THEN "清库存（侵权/违禁）"
                    WHEN 6 THEN "包材"
                    WHEN 7 THEN "断货"
                    WHEN 8 THEN "菜鸟模型"
                    WHEN 9 THEN "海外仓"
                    WHEN 10 THEN "亚马逊"
                    WHEN 11 THEN "下架（侵权/违禁）"
                    WHEN 12 THEN "IT冻结"
                    ELSE "" 
                END sales_status_name,
                t.eancode,
                t.asweight,
                t.baleweight,
                t.aslength taslength,
                t.aswide taswide,
                t.ashight tashight,
                t.balelength,
                t.balewide,
                t.balehight,
                t.color,
                t.type,
                t.shape,
                t.size,
                t.pattern,
                t.develop_link')
            ->where('s.sku',$sku)
            ->find();
    }

    /**
     * 获取编辑审核详细信息
     * @author LSH
     * @return array
     */
    public static function dbGetDataManufactureList($param, $titles = null, $function = null)
    {
        $where = [];
        $where['m.cancel'] = 0;
        $where['s.status'] = 1; // SKU状态为启用
        !empty($param['sku'])&&$where['s.sku'] = ['IN',explodeStr($param['sku'])];
        !empty($param['spu'])&&$where['s.spu'] = $param['spu'];
        !empty($param['skuName'])&&$where['s.name'] = ['like','%'.$param['skuName'].'%'];
        !empty($param['operator'])&&$where['m.operator'] = $param['operator'];
        !empty($param['handover_type'])&&$where['h.type'] = $param['handover_type'];
        !empty($param['status'])&&$where['m.manufacture_status'] = ['in',$param['status']];
        !empty($param['assign_time'])&&$where['m.assign_time'] = [['>=',substr($param['assign_time'],0,10).' 00:00:00'], ['<=',substr($param['assign_time'],-10,10).' 23:59:59'],'AND'];
        !empty($param['finish_time'])&&$where['m.finish_time'] = [['>=',substr($param['finish_time'],0,10).' 00:00:00'], ['<=',substr($param['finish_time'],-10,10).' 23:59:59'],'AND'];
        if(!empty($param['category'])){
            if($param['category'] == 1){
                $where['m.assign_id'] = ['<>',''];
            }else if($param['category'] == 2){
                $where['m.assign_id'] = '';
            }
        }
        !empty($param['handover_remarks'])&&$where['h.remarks'] = ['LIKE','%'.$param['handover_remarks'].'%'];

        $whereOr = '';
        if(!empty($param['handovertype'])){
            $whereOr = " h.type = '".$param['handovertype']."' OR m.manufacture_type = '".$param['handovertype']."'";
        }

        $table = $param['type'] == 'edit'?'manage_edit_audit':'manage_img_audit';
        $db = Db::table($table)
            ->alias('m')
            ->join('skus s','m.sku = s.sku','LEFT')
            ->join('sku_handover h','m.sku = h.sku','LEFT')
            ->field('
                    m.id,
                    m.sku,
                    m.grade,
                    m.operator,
                    m.proposer,
                    m.manufacture_type,
                    m.manufacture_status,
                    m.assign_time,
                    m.finish_time,
                    m.assign_id,
                    m.audit_remarks,
                    CASE 
                        m.manufacture_status
                        WHEN 1 THEN "待确定"
                        WHEN 2 THEN "待制作"
                        WHEN 3 THEN "待审核"
                        WHEN 4 THEN "已驳回"
                        WHEN 5 THEN "已制作"
                        WHEN 6 THEN "已完结"
                        ELSE "" 
                    END mstatus_name,
                    s.spu,
                    s.name,
                    CASE 
                        m.assign_id 
                        WHEN m.assign_id IS NULL THEN "老品"
                        ELSE "新品" 
                    END product_type,
                    h.remarks as handover_remarks,
                    h.type as handover_type,
                    h.quantity hquantity,
                    h.quantity_unit hquantity_unit,
                    h.developer hdeveloper,
                    h.logo hlogo,
                    h.creation_time,
                    h.imitation,
                    h.realpic,
                    h.research_links,
                    h.aslength,
                    h.aswide,
                    h.ashight,
                    CASE 
                        h.authorization
                        WHEN 0 THEN "未授权"
                        WHEN 1 THEN "已授权"
                        ELSE "" 
                    END authorization_name,
                    "'.$param['type'].'" as ttype ');

        !empty($where)&&$db->where($where);
        !empty($whereOr)&&$db->where($whereOr);

        $db->order('m.id','desc');
        return \app\common\service\ToolService::downOrReturn($db, $param, $titles, $function);
    }

    /**
     * 制作提交状态更改
     * @author LSH
     * @return array
     */
    public static function dbUpdateManufactureStatus($param)
    {
        Db::startTrans();
        try{
            $status = $param['assignid'] == 0?'5':'3';
            $data = ['audit_status'=>1,'manufacture_status'=>$status,'finish_time'=>date('Y-m-d H:i:s',time())];
            $where = ['sku'=>$param['sku'],'manufacture_status'=>['IN',[2,4]]];
            Db::table('manage_img_audit')->where($where)->update($data);

            //新品提交 更改交接状态为已制作
            // if($param['assignid']) Db::table('sku_handover')->where('sku',$param['sku'])->update(['status'=>4]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            returnJson(0,'操作失败');
        }
    }
}
